<template>
	<view>
		<view class="cu-list menu card-menu margin-top"  @tap="selectAddress()">
			<view class="cu-item padding arrow" style="min-height: 200rpx;" v-if="addr.name">
				<view class="cuIcon-locationfill text-blue" style="font-size: 50upx;min-height: 100rpx;"></view>
				<view class="content">
					<view class="text-cut">
						<text class="text-lg">{{addr.name}}</text>
						<text class="text-df text-grey" style="margin-left: 6upx;line-height: 40upx;">{{addr.mobile}}</text>
					</view>
					<view class="text-sm flex flex-direction" style="line-height: 32upx;font-weight: 600;">
						<view class="">{{addr.province || ''}}{{addr.city|| ''}}{{addr.county|| ''}}{{addr.town|| ''}}{{addr.addr|| ''}}</view>
					</view>
					<text class="text-xs" style="color: #ff7435;font-weight: 600;font-size: 22upx;">收货地址可更换</text>
				</view>
			</view>
			<view class="cu-item padding arrow" style="min-height: 200rpx;" v-else>
				<view class="cuIcon-locationfill text-blue" style="font-size: 50upx;"></view>
				<view class="content">
					<text class="text-sm margin-left ">暂无收货地址 ，您可以添加一个</text>
				</view>
			</view>
		</view>
		<view  v-for="(seller,index) in buylist" :key="index" v-if="buylist.length">
			<view class="cu-list menu card-menu margin-top">
				<view class="cu-item"  style="padding-top: 10px;">
					<view class="content">
						<text class="cuIcon-shop text-theme" style="font-size: 18px;"></text>
						<text class="text-black text-bold text-df">{{seller.shopr_name}}</text>
					</view>
				</view>
				<view class="cu-item" style="padding:6px 10px 10px;" v-for="(item,index1) in seller.sku_list" :key="index1" v-if="item.checked===1" :style="index1<seller.sku_list.length-1?'border-bottom:1px solid #eee;':''">
					<view class="cu-avatar radius goodsImg" :style="'background-image:url('+item.goods_image+');'"></view>
					<view class="content margin-left flex-column" style="height: 180upx;">
						<view class="flex" style="align-items: flex-start;">
							<view style="flex:1;">
								<view class="text-sm" style="line-height: 32upx;font-weight: 400;">{{item.name}}</view>
								<view class="text-gray">
									<text style="background-color: #eee;padding: 3px 4px;font-size: 22upx;">
										{{item['spec_list'][0].spec_value || '默认'}}
									</text>
								</view>
							</view>
							<view class="flex-column">
								<view class="text-gray text-sm">￥{{formatPrice(item.goods_type==='INDUCTION'?item.cost:izmember?item.cost:item.original_price)}}</view>
								<view class="text-gray text-sm" style="text-align: right;">x{{item.num}}</view>
							</view>
						</view>
						
						<view class="text-sm" style="color: #ff7435;font-weight: 600;font-size: 22upx;">发货时间: 付款后48小时内</view>
					</view>
				</view>
				<!-- <view class="cu-item arrow">
					<view class="content">
						<text class="text-cut text-sm">购买数量</text>
					</view>
					<view class="action">
						<van-stepper :value="buyNumber" input-width="24px" button-size="20px" min="1" max="99"  @plus="add" @minus="sub"/>
					</view>
				</view> -->
				<view class="cu-item">
					<view class="content">
						<text class="text-cut text-sm">支付配送</text>
						<!-- <text class="text-cut text-sm margin-left-sm">普通快递</text> -->
					</view>
					<view class="action">
						<text class="text-cut text-sm">{{checkoutParam.receive_time}}</text>
					</view>
				</view>
				<view class="cu-item">
					<view class="content">
						<text class="text-cut text-sm">配送方式</text>
						<text class="text-cut text-sm margin-left-sm">普通快递</text>
					</view>
					<view class="action">
						<text class="text-cut text-sm">快递  包邮</text>
					</view>
				</view>
				<view class="cu-item">
					<view class="content">
						<text class="text-cut text-sm">运费</text>
					</view>
					<view class="action">
						<text class="text-cut text-sm padding-right-xxl" v-if="freight">￥{{freight}}</text>
					</view>
				</view>
				<view class="cu-item">
					<view class="content">
						<text class="text-cut text-sm">店铺优惠</text>
					</view>
					<view class="action">
						<text class="text-cut text-sm">暂无</text>
					</view>
				</view>
				<view class="cu-item">
					<view class="content flex">
						<text class="text-cut text-sm margin-right-sm">订单备注</text>
						<input type="text" class="text-black text-sm align-end" v-model="marks" placeholder="选填,可填写订单备注" style="flex: 1;"/>
					</view>
				</view>
				<view class="cu-item" style="padding-bottom: 20px;">
					<view class="action text-sm flex justify-end align-center" style="width: 100%;vertical-align: center;">
						<!-- <view class="text-grey">共{{seller.sku_list.length}}件</view> -->
						<view class="text-black" style="margin-left: 6upx;">小计:</view>
						<view class="text-red text-df text-bold">￥{{formatPrice(seller.price.total_price)}}</view>
					</view>
				</view>
			</view>
		</view>
		
		<view class="blck"></view>
		<view class="cu-bar bg-white tabbar foot border shop">
			<view class="padding flex justify-end" style="width: 100%;">
				<view class="flex align-center">
					<!-- <view class="text-cut text-sm margin-right-xs">
						共{{buyNumber}}件,
					</view> -->
					<view class="text-sm">合计:</view><view class="text-bold text-lg text-red">￥{{formatPrice(sumPrice)}}</view>
				</view>
				<button class="cu-btn bg-red round shadow-blur margin-left" @tap="toPay">去支付</button>
			</view>
		</view>
	</view>
</template>

<script>
	import * as Checkout_API from '@/api/checkout.js' 
	import * as Address_API from '@/api/member/address.js' 
	import * as Trade_API from '@/api/trade.js' 
	import * as Cart_API from '@/api/cart.js' 
	import * as Foundation from '@/utils/Foundation.js'
	export default {
		data() {
			return {
				type:'',
				goods:{},
				buylist:[],		//订单列表
				goodsPrice:0.0,	//商品合计价格
				sumPrice:0.0,	//用户付款价格
				freight:0.00,	//运费
				note:'',		//备注
				int:1200,		//抵扣积分
				deduction:0,	//抵扣价格
				marks:'',
				buyNumber:1,
				price:0.00,
				checkoutParam:{},
				addr:{},//收货地址
				izmember:uni.getStorageSync('role').id?true:false
			};
		},
		onLoad(e) {
			this.GET_CheckoutParams()
		},
		onShow() {
			this.GET_CheckoutParams()
			//页面显示时，加载订单信息
			uni.getStorage({
				key:'buylist',
				success: (ret) => {
					this.buylist = ret.data.cart_list
					this.sumPrice = ret.data.total_price.total_price
				}
			});
		},
		onBackPress() {
			//页面后退时候，清除订单信息
			this.clearOrder()
		},
		filters: {
			toFixed:function(x) {
				return parseFloat(x).toFixed(2);
			}
		},
		methods: {
			formatPrice(price){
				return Foundation.formatPrice(price)
			},
			clearOrder(){
				uni.removeStorage({
					key: 'buylist',
					success: (res)=>{
						this.buylist = []
					}
				})
			},
			toPay(){
				if(this.addr.mobile){
					uni.showLoading({
						title:'正在提交订单...'
					})
					let client = 'MINI'
					Trade_API.CreateTrade(client).then(res =>{
						if(res.trade_sn){
							uni.setStorage({
								key:'paymentOrder',
								data:res,
								success: () => {
									uni.hideLoading();
									setTimeout(()=>{
										uni.redirectTo({
											url:'../pay/payment?amount='+this.sumPrice+'&sn='+res.trade_sn
										})
									},500)
								}
							})
						}
					})	
				}else{
					uni.showToast({
						icon:'none',title:'请选择收货地址'
					})
					setTimeout(()=>{
						this.selectAddress()
					},500)
				}
			},
			//选择收货地址
			selectAddress(){
				uni.navigateTo({
					url:'../member/address/address?izOrder=1'
				})
			},
			//减少数量
			sub(){
				if(this.buyNumber<=1){return}
				this.buyNumber--;
			},
			//增加数量
			add(){
				this.buyNumber++;
			},
			GET_CheckoutParams(){
				Checkout_API.getCheckoutParams().then(res =>{
					this.checkoutParam = res
					this.getDefaultAddr()
				})
			},
			getDefaultAddr(){
				Address_API.getAddr(this.checkoutParam.address_id).then(res =>{
					this.addr = res
				})
			}
		},
		beforeDestroy() {
			this.clearOrder()
		}
	}
</script>

<style lang="scss" scoped>
.no-border{
	border:none
}
/deep/.cu-list.menu>.cu-item:after {
	border-bottom: none;
	height: 60upx;
}
.addr{
	width: 86%;
	padding: 20upx 3%;
	margin: 30upx auto 20upx auto;
	box-shadow: 0upx 5upx 20upx rgba(0,0,0,0.1);
	border-radius: 20upx;
	display: flex;
	.icon{
		width: 80upx;
		height: 80upx;
		display: flex;
		align-items: center;
		image{
			width: 60upx;
			height: 60upx;
		}
	}
	.tel-name{
		width: 100%;
		display: flex;
		font-size: 32upx;
		.tel{
			margin-left: 40upx;
		}
	}
	.addres{
		width: 100%;
		font-size: 26upx;
		color: #999;
	}
}
.buy-list{
	width: 86%;
	padding: 10upx 3%;
	margin: 30upx auto 20upx auto;
	box-shadow: 0upx 5upx 20upx rgba(0,0,0,0.1);
	border-radius: 20upx;
	.row{
		margin: 30upx 0;
		.goods-info{
			width: 100%;
			display: flex;
			.img{
				width: 22vw;
				height: 22vw;
				border-radius: 10upx;
				overflow: hidden;
				flex-shrink: 0;
				margin-right: 10upx;
				image{
					width: 22vw;
					height: 22vw;
				}
			}
			.info{
				width: 100%;
				height: 22vw;
				overflow: hidden;
				display: flex;
				flex-wrap: wrap;
				position: relative;
				.title{
					width: 100%;
					font-size: 28upx;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					// text-align: justify;
					overflow: hidden;
				}
				.spec{
					font-size: 22upx;
					background-color: #f3f3f3;
					color: #a7a7a7;
					height: 40upx;
					display: flex;
					align-items: center;
					padding: 0 10upx;
					border-radius: 20upx;
					margin-bottom: 20vw;
				}
				.price-number{
					position: absolute;
					width: 100%;
					bottom: 0upx;
					display: flex;
					justify-content: space-between;
					align-items: flex-end;
					font-size: 28upx;
					height: 40upx;
					.price{
						color: #f06c7a;
					}
					.number{
						display: flex;
						justify-content: center;
						align-items: center;
						
					}
				}
			}
		}
	}
}
.order{
	width: 86%;
	padding: 10upx 3%;
	margin: 30upx auto 20upx auto;
	box-shadow: 0upx 5upx 20upx rgba(0,0,0,0.1);
	border-radius: 20upx;
	.row{
		margin: 20upx 0;
		height: 40upx;
		display: flex;
		.left{
			font-size: 28upx;
		}
		.right{
			margin-left: 40upx;
			font-size: 26upx;
			color: #999;
			input{
				font-size: 26upx;
				color: #999;
			}
		}
	}
}
.blck{
	width: 100%;
	height: 130upx;
}
.footer{
		width: 92%;
		padding: 0 4%;
		background-color: #fbfbfb;
		height: 100upx;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		font-size: 28upx;
		position: fixed;
		bottom: 0upx;
		z-index: 5;
		
		.settlement{
			width: 80%;
			display: flex;
			justify-content: flex-end;
			align-items: center;
			.sum{
				width: 50%;
				font-size: 28upx;
				margin-right: 10upx;
				display: flex;
				justify-content: flex-end;
				.money{
					font-weight: 600;
				}
			}
			.btn{
				padding: 0 30upx;
				height: 60upx;
				background-color: #f06c7a;
				color: #fff;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 30upx;
				border-radius: 40upx;
			}
		}
	}
.detail{
	width: 86%;
	padding: 10upx 3%;
	margin: 30upx auto 20upx auto;
	box-shadow: 0upx 5upx 20upx rgba(0,0,0,0.1);
	border-radius: 20upx;
	.row{
		height: 60upx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		.nominal{
			font-size: 28upx;
		}
		.content{
			font-size: 26upx;
			color: #f06c7a;
		}
	}
}
/deep/.cu-list.menu>.cu-item  {
min-height: 80rpx;
}
/deep/.cu-list.menu>.cu-item .content>text[class*=cuIcon] {
	width: 18px;
	text-align: left;
}
.goodsImg{
	width: 180upx;
	height: 180upx;
}
</style>
